<?php
require __DIR__ . '/../include/config.inc.php';
require __DIR__ . '/../include/utils.class.php';

use Silex\Provider\FormServiceProvider;
use Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
use Silex\Application;
use Silex\Provider\DoctrineServiceProvider;

$loader = require __DIR__ . '/../vendor/autoload.php';
$loader->add ( 'Controller', __DIR__ . '/../src' );
$loader->add ( 'Entity', __DIR__ . '/../src' );
$loader->add ( 'User', __DIR__ . '/../src' );

$app = new Silex\Application ();

$app->register ( new Silex\Provider\ServiceControllerServiceProvider () );
$app->register ( new Silex\Provider\TwigServiceProvider (), array (
                'twig.path' => __DIR__ . '/../views',
                'twig.options' => array (
                                'cache' => __DIR__ . '/../cache/',
                                'auto_reload' => true 
                ) 
) );

$app->register ( new FormServiceProvider () );
$app->register ( new Silex\Provider\TranslationServiceProvider (), array (
                'locale_fallbacks' => array (
                                'en' 
                ) 
) );

$config = new \Doctrine\ORM\Configuration ();
$config->setMetadataCacheImpl ( new \Doctrine\Common\Cache\ArrayCache () );
$driverImpl = $config->newDefaultAnnotationDriver ( array (
                __DIR__ . "/../htdocs/src" 
), false );
$config->setMetadataDriverImpl ( $driverImpl );

$config->setProxyDir ( __DIR__ . '/Proxies' );
$config->setProxyNamespace ( 'Proxies' );

$connectionOptions = array (
                'driver' => 'pdo_mysql',
                'host' => DB_HOST,
                'dbname' => DB_NAME,
                'user' => DB_USER,
                'password' => DB_PASS,
                'charset' => DB_CHAR 
);

$app ['em'] = \Doctrine\ORM\EntityManager::create ( $connectionOptions, $config );

$app->register ( new Silex\Provider\SecurityServiceProvider (), array (
                'security.firewalls' => array (
                                'install' => array (
                                                'pattern' => '^/install/',
                                                'form' => array (
                                                                'login_path' => '/install_login',
                                                                'check_path' => '/install/login_check' 
                                                ),
                                                'users' => $app->share ( function () use($app) {
                                                    return new User\UserProvider ( $app ['em'] );
                                                } ) 
                                ) 
                ) 
) );

//             $user = new \Entity\User();
//             $user->setAcct('luckyweida@gmail.com');
//             $user->setSalt(substr( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", mt_rand(0, 50) , 1) .substr( md5( time() ), 1));
//             $user->setPass(md5($user->getSalt() . '123456'));
//             $user->setCreatedOn(date('Y-m-d', time()));
//             $app ['em']->persist($user);
//             $app ['em']->flush();
            
//             var_dump($user);exit;

$app->mount ( '/install', new Controller\Install\InstallCSP ( $app, array () ) );
$app->mount ( '/install_login', new Controller\Install\InstallLoginCSP ( $app, array () ) );

$app->mount ( '/account/map', new Controller\Account\MapCSP ( $app, array () ) );

$app->mount ( '', new Controller\Install\DefaultCSP ( $app, array () ) );

$app->error ( function (\Exception $e, $code) use($app) {
    if ($code == 404) {
        return new Symfony\Component\HttpFoundation\Response ( $app ['twig']->render ( 'install/404.twig', array () ), 404 );
    }
} );

$app->run ();
